<?php

class SlideController extends AdminController {

    // public $layouts = '//layouts/column1';
    public $parentUrl;

    public function init(){
        parent::init();
        $this->parentUrl = $this->module->id .'/'.Yii::app()->getController()->getId();
    }
    public function actionIndex() {
        $model = Slide::model()->findAll();
        $this->render('index', array(
            'data' => $model, 
            'parentUrl' => $this->parentUrl
        ));
    }

    public function actionCreate() {
        try{
            $model = new Slide();
            if (isset($_POST['Slide'])) {
                //Create new album, gallery
                $model->attributes = $_POST['Slide'];
                $file = CUploadedFile::getInstance($model,'name');
                if($file){
                    $fileName = 'images_' . md5(time()) . '.' . $file->extensionName;
                    $model->name = $fileName;
                    $file->saveAs(Yii::app()->basePath . "/../images/$fileName");
                }
                $model->validate();
                if($model->save()){
                    $this->redirect(Yii::app()->createAbsoluteUrl('admin/slide/index'));
                }
            }
            $this->render('create', array('model' => $model));
        }catch(Exception $e){
            Yii::log("Invalid request(Language). Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionUpdate($id){
        try{
            $model = $this->loadModel($id);
            $older_photo = $model['name'];
            if (isset($_POST['Slide'])) {
                $model->attributes = $_POST['Slide'];
                $file = CUploadedFile::getInstance($model,'name');
                if($file !== NULL){
                    $fileName = 'images_' . md5(time()) . '.' . $file->extensionName;
                    $model->name = $fileName;
                    $saveFile = $file->saveAs(Yii::app()->basePath . "/../images/$fileName");
                    if($saveFile && $older_photo !== '' && file_exists(Yii::app()->basePath . "/../images/$older_photo")){
                        unlink(Yii::app()->basePath . "/../images/$older_photo");
                    }
                }else{
                    $model['name'] = $older_photo;
                }
                $model->validate();
                if($model->save()){
                    $this->redirect(Yii::app()->createAbsoluteUrl('admin/slide/index'));
                }
            }
            $this->render('update', array('model' => $model));
        }catch(Exception $e){
            Yii::log("Invalid request(Language). Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionDelete($id){
        $model = $this->loadModel($id); 
        $older_photo = $model['name'];
        if($model){
            if($model->delete()){               
                if(file_exists(Yii::app()->basePath . "/../images/$older_photo")){
                    unlink(Yii::app()->basePath . "/../images/$older_photo");
                }
                $this->redirect(Yii::app()->createAbsoluteUrl('admin/slide'));
            }
        }
    }

    public function loadModel($id){
        try{
            $model = Slide::model()->findByPk($id);
            if($model)
                return $model;
        }catch(Exception $e){
            Yii::log("Invalid request(Language). Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

}

?>